package edu.ucsc.mit2008.swan.core;

import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/**
 * Activator for the Swan core.
 * 
 * @author Thushara Sanjeewa Ramasinghe
 *
 */
public class CorePlugin implements BundleActivator {

	private static BundleContext context;
	private static CorePlugin plugin;
	private SwanWindow window;
	
	public static BundleContext getContext() {
		return context;
	}
	
	public CorePlugin(){
		plugin = this;
	}
	
	public static CorePlugin getDefault(){
		return plugin;
	}
	
	public Bundle getBundle(){
		if(context != null)
			return context.getBundle();
		return null;		
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void start(BundleContext bundleContext) throws Exception {
		CorePlugin.context = bundleContext;
		System.out.println("Opening Editor");
		Display.getDefault().syncExec(new Runnable() {
			
			@Override
			public void run() {
				window = new SwanWindow(null);
				window.setBlockOnOpen(true);
				window.open();		
			}
		});
	}

	/**
	 * {@inheritDoc}
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		CorePlugin.context = null;
		System.out.println("Closing Editor");
		Display.getDefault().syncExec(new Runnable() {
			
			@Override
			public void run() {
				window.close();		
			}
		});
	}

}
